<template>
  <div class="service">
    <div class="header">
      <van-swipe v-if="bannerList.length" class="my-swipe" height="155" :autoplay="3000" indicator-color="white" v-show="bannerList.length">
        <van-swipe-item v-for="(item, index) in bannerList" :key="index" @click="bannerClick(item.linkUrl)">
          <img :src="item.pictureUrl" alt="" />
        </van-swipe-item>
      </van-swipe>
      <ul class="consult">
        <li @click="call(servicePhone)">
          <img src="@assets/images/icon_tel.png" alt="" />
          <div class="right">
            <p>电话咨询</p>
            <p>{{ servicePhone }}</p>
          </div>
        </li>
        <li @click="call(consultPhone)">
          <img src="@assets/images/icon_consult.png" alt="" />
          <div class="right">
            <p>专属顾问</p>
            <p>一对一专业服务</p>
          </div>
        </li>
      </ul>
    </div>
    <div class="content">
      <div class="hot-product">
        <h3>热门产品</h3>
        <div class="swiper-container hot-list" ref="swiperBox">
          <div class="swiper-wrapper">
            <div class="swiper-slide hot-option" v-for="(item, index) in hotList" :key="index" @click="routerTo(`${item.webUrl}?id=${item.id}`)">
              <img v-if="index === 0" class="hot-icon" src="@assets/images/hot_icon.png" alt="" />
              <p class="title">{{ item.name }}</p>
              <p class="desc">{{ item.tag }}</p>
              <img class="logo" :src="item.icon" alt="" />
            </div>
          </div>
        </div>
        <ul class="tabs">
          <li v-for="(item, index) in categoryList" :key="index" :class="category === item.id ? 'active' : ''" @click="category = item.id">{{ item.name }}</li>
        </ul>
        <div class="swiper-container tab-list" ref="tabBox">
          <div class="swiper-wrapper">
            <div class="swiper-slide tab-option" v-for="(item, index) in tabList" :key="index" :class="categoryId === item.id ? 'active' : ''" @click="categoryId = item.id">{{ item.name }}</div>
          </div>
        </div>
        <ul class="product-list" v-if="productList.length">
          <li class="product-option" v-for="(item, index) in productList" :key="index" @click="routerTo(`/loanProductDetail?id=${item.id}`)">
            <div class="product-header">
              <span class="product-name">{{ item.name }}</span>
              <div class="tags" v-if="item.tag">
                <span v-for="(todo, k) in item.tag.split(',')" :key="k">{{ todo }}</span>
              </div>
            </div>
            <ul class="product-detail">
              <li>
                <p class="val">{{ item.quota }}</p>
                <p class="desc">最高额度</p>
              </li>
              <li>
                <p class="val">{{ item.interest }}</p>
                <p class="desc">利息</p>
              </li>
              <li>
                <van-button class="btn" color="linear-gradient(to right, #ee7b34, #ea4f3c)" round>立即申请</van-button>
                <p class="desc red">{{ item.applyNumber }}已申请</p>
              </li>
            </ul>
          </li>
        </ul>
        <Empty v-else></Empty>
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from '@assets/js/swiper-3.4.2.min';
import { Swipe, SwipeItem, Button, Toast } from 'vant';
import Empty from '@components/empty';

import { picture_getPictureList, user_getSalesmanPhone, product_getHotProductList, product_getCategoryList, product_getCategoryProductList } from '@api/service';
export default {
  components: {
    [Button.name]: Button,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    Empty
  },
  data() {
    return {
      category: '', //一级分类的id
      categoryId: '', //二级分类的id
      bannerList: [],
      consultPhone: '',
      servicePhone: '4008-955-187',
      hotList: [],
      categoryList: [],
      productList: []
    };
  },
  mounted() {
    var swiper = new Swiper(this.$refs.tabBox, {
      spaceBetween: 0,
      slidesPerView: 'auto',
      freeMode: true,
      observer: true,
      observeParents: true
    });
    this.getBannerList();
    this.getCategoryList();
    this.getConsultPhone();
    this.getHotList();
  },
  watch: {
    categoryId: {
      handler: function(newVal, oldVal) {
        this.getProductList();
      }
    }
  },
  computed: {
    tabList() {
      let arr = [];
      if (this.categoryList.length) {
        arr = this.categoryList.find(item => {
          return item.id === this.category;
        }).secondList;
        this.categoryId = arr[0].id;
      }
      return arr;
    }
  },
  methods: {
    async getCategoryList() {
      let res = await product_getCategoryList();
      if (res.code === 200) {
        this.categoryList = res.data;
        this.category = this.categoryList[0].id;
      } else {
        Toast(res.msg);
      }
    },
    async getHotList() {
      let res = await product_getHotProductList();
      if (res.code === 200) {
        this.hotList = res.data.map(item => {
          let obj = {};
          item.attrList.forEach(todo => {
            obj[todo.key] = todo.value;
          });
          return {
            name: item.name,
            id: item.id,
            ...obj
          };
        });
        this.$nextTick(() => {
          var swiper = new Swiper(this.$refs.swiperBox, {
            autoplay: false,
            observer: true,
            slidesPerView: 3.1,
            observeParents: true,
            spaceBetween: 15
          });
        });
      } else {
        Toast(res.msg);
      }
    },
    async getProductList() {
      let res = await product_getCategoryProductList({ categoryId: this.categoryId });
      if (res.code === 200) {
        this.productList = res.data.map(item => {
          let obj = {};
          item.attrList.forEach(todo => {
            obj[todo.key] = todo.value;
          });
          return {
            name: item.name,
            id: item.id,
            ...obj
          };
        });
      } else {
        Toast(res.msg);
      }
    },
    bannerClick(url) {
      this.$router.openPage(url);
    },
    call(phone) {
      window.location.href = `tel:${phone}`;
    },
    async getConsultPhone() {
      let res = await user_getSalesmanPhone();
      if (res.code === 200) {
        this.consultPhone = res.data;
      } else {
        Toast(res.msg);
      }
    },
    async getBannerList() {
      let res = await picture_getPictureList({ type: 1 });
      if (res.code === 200) {
        this.bannerList = res.data;
      } else {
        Toast(res.msg);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.service {
  background: #fff;
}
.header {
  overflow: hidden;
  background: #fff;
  padding: 0 30px;
}
.my-swipe {
  margin-top: 20px;
  border-radius: 10px;
  .van-swipe-item {
    img {
      width: 100%;
    }
  }
}
.content {
  overflow: hidden;
  padding: 0 30px;
  background: #fff;
  h3 {
    position: relative;
    display: inline-block;
    font-size: 36px;
    color: #121c32;
    font-weight: bold;
    &:after {
      content: '';
      position: absolute;
      bottom: 3px;
      left: -1.5%;
      width: 103%;
      height: 10px;
      background: #ef7c38;
      opacity: 0.5;
    }
  }
}
.hot-list {
  position: relative;
  left: -30px;
  right: 0;
  width: 100vw;
  padding: 30px;
  margin: 15px 0 45px;
  box-sizing: border-box;
}
.hot-option {
  position: relative;
  height: 210px;
  padding-top: 25px;
  background: #fff;
  box-shadow: 0 0 32px rgba(169, 185, 221, 0.36);
  border-radius: 10px;
  text-align: center;
  box-sizing: border-box;
  .title {
    font-size: 28px;
    color: #121c32;
  }
  .desc {
    font-size: 24px;
    color: #9da1ac;
    margin: 15px 0 25px;
  }
  .logo {
    width: 73px;
    height: 73px;
  }
  .hot-icon {
    position: absolute;
    right: 5px;
    top: 95px;
    width: 54px;
    height: 36px;
  }
}
.tabs {
  display: flex;
  align-items: center;
  li {
    position: relative;
    display: inline-block;
    color: #121c32;
    font-size: 34px;
    margin-right: 30px;
    &.active {
      font-size: 36px;
      font-weight: bold;
      &:after {
        content: '';
        position: absolute;
        bottom: 3px;
        left: -1.5%;
        width: 103%;
        height: 10px;
        background: #ef7c38;
        opacity: 0.5;
      }
    }
  }
}
.tab-list {
  position: relative;
  margin: 30px 0;
  &:after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: 150px;
    background: linear-gradient(to left, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0.3));
    z-index: 100;
  }
}
.tab-option {
  width: auto;
  padding: 12px 20px;
  font-size: 28px;
  color: #68707f;
  border-radius: 24px;
  &.active {
    background: #fcedea;
    color: #fd4629;
    font-weight: bold;
  }
}
.product-list {
  margin-top: 40px;
}
.product-option {
  padding: 30px;
  background: #ffffff;
  box-shadow: 0px 18px 32px 0px rgba(169, 185, 221, 0.36);
  border-radius: 14px;
  margin-bottom: 40px;
  box-sizing: border-box;
  .product-header {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
  }
  .product-name {
    font-size: 32px;
    color: #121c32;
    font-weight: bold;
    margin-right: 30px;
  }
  .tags {
    display: flex;
    align-items: center;
    span {
      line-height: 1;
      font-size: 24px;
      color: #818b9d;
      background: #e9ecf3;
      padding: 5px 10px;
      border-radius: 4px;
      margin-right: 15px;
      &:nth-child(even) {
        background: #fdf2e8;
        color: #fe7310;
      }
    }
  }
  .product-detail {
    margin-top: 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    li {
      text-align: center;
    }
    .val {
      color: #121c32;
      font-size: 36px;
      font-weight: bold;
    }
    .desc {
      font-size: 24px;
      color: #818b9d;
      margin-top: 23px;
    }
    .btn {
      width: 155px;
      height: 54px;
      padding: 0;
      font-weight: bold;
      line-height: 54px;
      font-size: 24px;
      margin-top: -10px;
    }
    .red {
      color: #fd402c;
    }
  }
}
.consult {
  display: flex;
  justify-content: space-between;
  margin: 50px 0;
  li {
    display: flex;
    align-items: center;
    color: #fff;
    width: 313px;
    height: 127px;
    border-radius: 14px;
    &:first-child {
      background: #fa6257;
    }
    &:last-child {
      background: #fdbd42;
    }
    img {
      width: 46px;
      margin: 0 25px 0 36px;
    }
    .right {
      font-size: 22px;
      p:first-child {
        font-weight: bold;
        font-size: 28px;
        margin-bottom: 10px;
      }
    }
  }
}
</style>
